using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using System.Collections.Specialized;
using MCMLXVII.Tools.OrganizationChart.MembersClasses;

namespace MCMLXVII.Tools.OrganizationChart.SmartEditors
{
    public partial class ucSmartNodeEditor : ucSmartBaseEditor 
    {
        public override event EventHandler<MemberUpdatedEventsArgs> EditorClosed;
        public override event EventHandler<MemberUpdatedEventsArgs> MemberUpdated;

        private NodeEditorHelper _Node;
        private int _FatherNodeID;
        public ucSmartNodeEditor(NodeEditorHelper Node, int FatherNodeID):base()
        {
            InitializeComponent();
            _Node = Node;
            _FatherNodeID = FatherNodeID;
            base.CancelRequested += new EventHandler(ucSmartNodeEditor_CancelRequested);
            base.SaveRequested += new EventHandler(ucSmartNodeEditor_SaveRequested);
        }

        void ucSmartNodeEditor_SaveRequested(object sender, EventArgs e)
        {
            if (MemberUpdated != null)
            {
                for(int i = 0; i<grdTranslations.Rows.Count; i++)
                {
                    foreach (StringTranslations prop in _Node.NameTranslated.Collection)
                    {
                        if (prop.Language == grdTranslations.Rows[i].Cells[0].Value.ToString() )
                            prop.Translation = grdTranslations.Rows[i].Cells[1].Value.ToString();
                    }
                }
                MemberUpdated.Invoke(this, new MemberUpdatedEventsArgs(_FatherNodeID, _Node));
                base.DisableUpdate();

            }
        }
        
        
        void ucSmartNodeEditor_CancelRequested(object sender, EventArgs e)
        {
            if (EditorClosed != null)
            {
                EditorClosed.Invoke(this, new MemberUpdatedEventsArgs(_Node.ID));
            }
        }


        private void ucSmartNodeEditor_Load(object sender, EventArgs e)
        {
            txtNodeID.Text = _Node.ID.ToString();
            txtNodeName.Text = _Node.Name;
            if (_Node.OwnerID != null)
            {
                txtOwnerID.Text = _Node.OwnerID.MemberID.ToString();
                txtOwnerName.Text = _Node.OwnerID.Description;
            }
            txtUserInfo.Text = "...";

            txtADInfo.Text = _Node.DirectoryServiceConfigInfo;

            Dictionary<string,string> Langs = MemberBase.LanguageList();
            int i = 0;
            foreach (string key in Langs.Keys)
            {
                string val = "";
                foreach (StringTranslations  prop in _Node.NameTranslated.Collection )
                {
                    if (prop.Language == key)
                        val =  prop.Translation;
                }

                grdTranslations.Rows.Add(key, val);
            }
            base.RelocateButtons(panel4.Top+panel4.Height);
        }


        private void btnSelUser_Click(object sender, EventArgs e)
        {
            frmMembersSelector frmDialog = new frmMembersSelector();
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.Cancel)
            {
                txtOwnerID.Text = frmDialog.ActualMember.MemberID.ToString();
                txtOwnerName.Text = frmDialog.ActualMember.Description;
                txtUserInfo.Text = "...";
                _Node.OwnerID = frmDialog.ActualMember;
                base.EnableUpdate();
            }
        }

        private void btnSelectDir_Click(object sender, EventArgs e)
        {
            frmDirectoryService frmDialog = new frmDirectoryService();
            frmDialog.BaseValue = txtADInfo.Text;
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.Cancel)
            {
                txtADInfo.Text = frmDialog.BaseValue;
                _Node.DirectoryServiceConfigInfo = frmDialog.BaseValue;
                base.EnableUpdate();
            }
        }

        private void txtNodeName_TextChanged(object sender, EventArgs e)
        {
            _Node.Name = txtNodeName.Text;
            base.EnableUpdate();
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
    }
}
